package demo1;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 石方旭
 * Date: 2022-08-11
 * Time: 16:00
 */
public class TestDemo5 {
    /**
     * 使用ThreadPoolExecutor创建一个忽略最新任务的线程池，创建规则：
     * 1.核心线程数为5
     * 2.最大线程数为10
     * 3.任务队列为100
     * 4.拒绝策略为忽略最新任务
     */
    public static void main1(String[] args) {
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(5,
                10,
                1L, TimeUnit.SECONDS,new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.DiscardPolicy());

        for (int i = 0; i < 2000; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "已执行.");
            },"thread-" + (i + 1)).start();
        }
    }
    public static void main(String[] args) {
        ExecutorService threadPoolExecutor = new ThreadPoolExecutor(5,
                10,
                1L, TimeUnit.SECONDS,new ArrayBlockingQueue<>(100),
                new ThreadPoolExecutor.DiscardPolicy());
        for(int i =0;i<100;++i){
            threadPoolExecutor.submit(new Runnable() {
                @Override
                public void run() {
                    System.out.println(Thread.currentThread().getName());
                }
            });
        }
    }
}
